package storyforkids.main;

import storyforkids.io.StatementsManager;
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import com.marcnuri.MAndFileBrowser.R;


public class StoryForKids extends Activity implements AnimationListener {
    
	private StatementsManager stateMng = new StatementsManager();
	private float Width,Height;

	private Animation faceIn,faceOut;
	
	private TextView tv;
	
	private String statement = "";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.storyforkids);
  
        Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
		Width = display.getWidth(); 
		Height = display.getHeight();
		
		faceOut = AnimationUtils.loadAnimation(this, R.anim.fade_out);
		faceIn = AnimationUtils.loadAnimation(this, R.anim.fade_in);
		
		faceOut.setAnimationListener(this);
        
//      Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/BPreplay.otf");
//		Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/CHILLER.TTF");
//      Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/Ravie.ttf");
//		Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/CAW.TTF");
//		Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/sahara.ttf");
        tv = (TextView) findViewById(R.id.txtStoryForKids);
//        tv.setTextSize(60);
//        tv.setTypeface(tf);
        
        TextView txtStoryForKids = (TextView) findViewById(R.id.txtStoryForKids);
        StoryForKidsApplication app = (StoryForKidsApplication) getApplicationContext();
        stateMng.initStatements(app.currentFile);
        stateMng.setCurrentPage(app.currentPage);
        statement = stateMng.statements.get(stateMng.i);
        tv.setText(statement);
        setKeepScreenOn(this,true);
        
    }
    
    private void setKeepScreenOn(Activity activity, boolean keepScreenOn) {
	    if(keepScreenOn) {
	      activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
	    } else {
	      activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
	    }
	}
    
    @Override
	public boolean onTouchEvent(MotionEvent e) {
    	if (e.getAction() == MotionEvent.ACTION_DOWN){
	//    	findViewById(R.id.txtStoryForKids).startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_out));
	    	tv.startAnimation(faceOut);
	    	if (e.getX()>Width/2){
	    		statement = stateMng.next();
	    	}else{
	    		statement = stateMng.back();
	    	}
    	}
    	return super.onTouchEvent(e);
    }

	public void onAnimationEnd(Animation arg0) {
   		tv.setText(statement);
   		tv.startAnimation(faceIn);
	}

	public void onAnimationRepeat(Animation arg0) {
		// TODO Auto-generated method stub
		
	}

	public void onAnimationStart(Animation animation) {
		// TODO Auto-generated method stub
		
	}
     
	@Override
	protected void onDestroy() {
		StoryForKidsApplication app = (StoryForKidsApplication) getApplicationContext();
		app.currentPage = stateMng.getCurrentPage();
		super.onDestroy();
		
	}
}